define(function(require,exports,module) {
    var myPub = require('./public');

    var oGoodsShowBox = $('.js_goodsShowBox');
    var oCategorysGroupBox = $('.js_categorys_group');          // 分类容器
    var oContain = $('.js_contain');

    var oLoadingBtn = $('.js_loadingBtn');     //加载中和没有更多
    var oSideBox = $('.js_sideBox');
    var oGoTop = $('.js_goTop');

    var lazLoading = new myPub.lazLoadingImg();

    // 获取产品数据的路径
    var goodsDateUrl = '/supplier/supplierGoodsListSearch';

    //获取路径上的参数--分类id
    var paramObj = {};   //保存当前路径的对象


    // 回到过顶部
    oGoTop.hammer().on('tap', function () {
        oContain.eq(0).stop().animate({scrollTop: '0'}, 250);

    });

    module.exports = {
        init:function(data,names){

            supplierGoods.getCateId();

            supplierGoods.getDataList();

            supplierGoods.ScrollLoad();

            // 产品懒加载  异步加载 调用这个
            // myPub.lazLoadingImgEchoAbs(oCategorysGroupBox[0]);

            // categorysGoods.ScrollLoad()
            checkEqu.ChangeAHerf(oCategorysGroupBox.find('li a'))
            //categorysGoods.getDataList()
        }
    };

    var supplierGoods = {
        loadOff: [false],       // 加载开关
        start: 0,
        limit: 10,
        loadComplete: false,
        isLoading: false,
        firstLoad: true,
        getDataList: function (fn) {
            var _this = this;
            G_JSON_POST(goodsDateUrl,{
                supplierId: paramObj.supplierId,
                categoryId: paramObj.categoryId,
                start: _this.start,
                limit: _this.limit,

            },function (res) {
                // console.log(res,res == true);
                var result = res;
                // console.log('jjjj',result == true);
                if(result && result.length > 0){
                    _this.firstLoad = false;
                    if(result.length >= _this.limit){
                        oLoadingBtn.hide().eq(1).show();
                    }
                    else {
                        _this.loadComplete = true;
                        oLoadingBtn.hide().eq(0).show();
                    }
                   $('.js_goodsList').append(_this.GoodsLiHtml(result));
                    // myPub.lazLoadingImgEchoAbs(oCategorysGroupBox[0]);
                    //产品懒加载  异步加载 调用这个
                    lazLoading.DestroyLazLoadingImg();
                    lazLoading.AddLazLoadingImg(oCategorysGroupBox[0]);
                }
                else {
                    if(_this.firstLoad){
                      //    加空白页
                        oGoodsShowBox.hide();
                        oContain.append(_this.wrongPageHtml());
                    }
                    else {
                        oLoadingBtn.hide().eq(0).show();
                        return;
                    }
                }
            }, function () {
                // start
            }, function () {
                // end
                // myPub.lazLoadingImgEchoAbs(oCategorysGroupBox[0]);
            });
        },

        GoodsLiHtml:function (data) {
            var shtml = '';
            var _this = this;

            for (var i=0; i<data.length; i++) {
                var item = data[i];
                var asideHtml = item.isTuangou ? _this.pinAsideHtml(item) : _this.goodsAsideHtml(item);
                var firstGueeHtml = item.firstGueeImagePath ? '<img src="'+ HTTP_IMAGE_URL + item.firstGueeImagePath+'" alt="" class="quanpei"/>' : '';
                shtml +=  '<li>'+
                            '    <a class="BoxSelf" href="' + BASE + '/h/goodspay/goodsView/'+ item.goodsSn +'">'+
                            '         <figure>'+
                            '             <img class="js_lazyImg" data-echo="'+ HTTP_IMAGE_URL + item.showImage + '?' + smallImgArg + '" src="'+ BASE +'/static_v4/image/loadImg.jpg">'+
                            firstGueeHtml+
                            '         </figure>'+
                            asideHtml+
                            '    </a>'+
                            '</li>';
            }
            return shtml;
        },

        pinAsideHtml: function (data) {
            var _this = this;
            var shtml = '';
            var title = data.title ? data.title : '';
            shtml =  '<aside class="priceArea VBox Bflex1">'+
                     '     <div class="Bflex1 priceArea_name">'+
                     '          <h4>'+ data.name +'</h4>'+
                     '          <p class="smallFont_tip">'+ title +'</p>'+
                     '          <div class="tuangouPeo">'+
                     '               <span class="cutNum">拼单立省'+ data.salePrice +'-'+ data.tuangouPrices +'元</span>'+
                     '          </div>'+
                     '     </div>'+
                     '     <div class="priceArea_price">'+
                     '          <p class="HBox priceAreaBox">'+
                     '               <span class="goodsPrice">￥'+ _this.toFix(data.tuangouPrices) +'</span>'+
                     '               <span class="tuangouOldPrice">￥'+ _this.toFix(data.salePrice) +'</span>'+
                     '          </p>'+
                     '     </div>'+
                     ' </aside>';
            return shtml;
        },

        goodsAsideHtml: function (data) {
            var _this = this;
            var shtml = '';
            var title = data.title ? data.title : '';
            shtml =  '<aside class="priceArea VBox Bflex1">'+
                     '      <div class="Bflex1 priceArea_name">'+
                     '           <h4>'+ data.name +'</h4>'+
                     '           <p class="smallFont_tip">'+ title +'</p>'+
                     '       </div>'+
                     '       <div class="priceArea_price">'+
                     '            <p class="HBox priceAreaBox">'+
                     '                 <span class="goodsPrice">￥'+ _this.toFix(data.salePrice) +'</span>'+
                     '            </p>'+
                     '       </div>'+
                     '</aside>';
             return shtml;
        },

        //空白页
        wrongPageHtml: function () {
            var shtml = '';

            shtml = '<div class="noContent VBox BoxCenter">'+
                '     <figure class="noContent_image">'+
                '          <img src="/static_v4/image/wonderful/loadingpage_nodata.png">'+
                '     </figure>'+
                '     <p class="noContent_text">页面暂无内容显示</p>'+
                '</div>';

            return shtml;
        },

        ScrollLoad: function () {
            var _this = this;
            var scrollTime = null;
            var minScrHeight = 300;
            oContain.off('scroll');
            oContain.on('scroll', function () {
                // console.log(';;;',oContain.scrollTop());
                var afterScroll = oContain.scrollTop();

                _this.ShowGoTop(afterScroll);
                // 滚动页面距离底部300像素 延迟150毫秒加载数据
                clearTimeout(scrollTime);
                scrollTime = setTimeout(function() {
                    if(oContain[0].scrollHeight - oContain.scrollTop() - oContain.height() < minScrHeight) {
                        // categorysGoods.getDataList()
                        if(!_this.loadComplete){
                            if(!_this.isLoading){
                                supplierGoods.start = supplierGoods.start + supplierGoods.limit;
                                supplierGoods.getDataList()
                            }
                        }
                    }
                },150);
            })
        },

        // 显示回到顶部按钮
        ShowGoTop: function (scrollTop) {
            scrollTop > 300 ? oSideBox.stop().animate({'right': 0}, 300) : oSideBox.stop().animate({'right': '-3.5rem'}, 300);
        },



        //获取路径上的参数，分类id
        getCateId: function() {
                var url = location.search; //获取url中"?"符后的字符串包括‘？’ ，window.location.href获取的是url完整的字符串
                var theParam = new Object();
                if (url.indexOf("?") != -1) { //确保‘？’不是最后一个字符串，即携带的参数不为空
                    var str = url.substr(1); //substr是字符串的用法之一，抽取指定字符的数目，这里抽取？后的所有字符
                    strs = str.split("&"); //将获取到的字符串从&分割，输出参数数组，即输出[参数1=xx,参数2=xx,参数3=xx,...]的数组形式
                    for(var i = 0; i < strs.length; i ++) { //遍历参数数组
                        theParam[strs[i].split("=")[0]]= unescape(strs[i].split("=")[1]); //这里意思是抽取每个参数等号后面的值，unescape是解码的意思
                    }
                }
            paramObj = theParam;

        },


        //保留两位数
        toFix: function (str,num) {
            num = num ? num : 2;
            return str.toFixed(num);
        }


    }

    var checkEqu = {
        hrefName: [BASE + '/h/goodspay/goodsView/', 'sf://goodsDetail?goodsSn='],
        ChangeAHerf: function (oHotList_a) {
            var _this = this;
            var _Num = 0;
            var u = navigator.userAgent;
            //var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
            var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端

            //检测室安卓还是ios
            if(u.search('origin/sfddjapp') > 0) {
                if(isIOS) {
                    _Num = 1;
                    oHotList_a.each(function(i) {
                        oHotList_a.eq(i).attr('href', _this.hrefName[_Num] + oHotList_a.eq(i).attr('_href')  )
                    })
                }
                else {
                    oHotList_a.each(function(i) {
                        _this.AndroidClick(oHotList_a.eq(i),oHotList_a.eq(i).attr('_href'))
                    })
                }
            }
            else {
                _Num = 0;
                oHotList_a.each(function(i) {
                    oHotList_a.eq(i).attr('href', _this.hrefName[_Num] + oHotList_a.eq(i).attr('_href')  )
                })
            }
        },
        AndroidClick: function (obj,arg) {
            obj.on('click',function() {
                android.goodsDetail(arg)
            })
        }
    }
});